package br.ucb.mercadojogador.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;


import javax.faces.convert.ConverterException;

import br.ucb.mercadojogador.HIB.TimeHIB;
import br.ucb.mercadojogador.beans.Time;

@FacesConverter (value="TimeConverter")
public class TimeConverter implements Converter {
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value)  throws ConverterException {
		TimeHIB timeDao = new TimeHIB();
		if (value == null)
			return null;
		try {
			return timeDao.consultar(Integer.parseInt(value));
		} catch (Exception e) {
			e.printStackTrace();
			return new Object();
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if (object == null)
			return null;
		Time time = (Time) object;		
		return Integer.toString(time.getIdTime());
	}

}
